package task

import (
	"hft/Database/admin/user_db"
	"hft/global"
	"sync"
)

// 保护期更新

var (
	mu            sync.Mutex
	isTaskRunning bool
)

type ProTectTime struct {
	Spec string
}

// 更新保护期
func (*ProTectTime) UpdateProTime() {
	mu.Lock()
	defer mu.Unlock()
	if isTaskRunning {
		// 任务还在执行
		return
	}
	isTaskRunning = true
	defer func() {
		isTaskRunning = false
	}()
	global.OC_DB.Model(&user_db.User{}).Where("release_time IS NOT NULL").Where("is_complete = ?", 0).Where("release_time < NOW()").Updates(map[string]interface{}{
		"manager_id":   0,
		"manager_name": "",
	})
}

// 线程实现判断该用户是否
// func IsInProtect(cityList []*admin.CitySetting, user *admin.User, db *gorm.DB) {

// 	var setting = FindCity(cityList, int(user.City))

// 	switch user.Level {
// 	case "A":
// 		Over(user.BrokerTime, float64(setting.LevelA), user.ID, db)
// 	case "B":
// 		Over(user.BrokerTime, float64(setting.LevelB), user.ID, db)
// 	case "C":
// 		Over(user.BrokerTime, float64(setting.LevelC), user.ID, db)
// 	case "D":
// 		Over(user.BrokerTime, float64(setting.LevelD), user.ID, db)
// 	}

// }

// 找出城市
// func FindCity(cityList []*admin.CitySetting, region int) *admin.CitySetting {
// 	for _, v := range cityList {
// 		if v.Region == region {
// 			return v
// 		}
// 	}

// 	return nil
// }

// 判断是否过期
// func IsOverTime(myTime time.Time, day float64) bool {
// 	var overtime = day * 24 * 60 * 60 * 1000

// 	var inTime = myTime.UnixMilli()
// 	var nowTime = time.Now().UnixMilli()
// 	if nowTime-inTime >= int64(overtime) {
// 		return false
// 	}

// 	return true
// }

// func Over() {

// }

var ProTectTimeTask = &ProTectTime{Spec: "0 */3 * * * ?"}
